<template>
  <div class="stats-view">
    <div class="filter-container">
      <el-form :inline="true">
        <el-form-item label="时间范围">
          <el-date-picker v-model="timeRange" type="daterange" range-separator="至" start-placeholder="开始日期"
            end-placeholder="结束日期" value-format="YYYY-MM-DD" />
        </el-form-item>

        <el-form-item>
          <el-button type="primary" @click="fetchData">查询</el-button>
        </el-form-item>
      </el-form>
    </div>

    <div class="charts-container">
      <div class="chart-card">
        <h3>用户活跃度</h3>
        <template v-if="chartLoading">
          <el-skeleton :rows="6" animated />
        </template>
        <div v-else ref="userActivityChart" style="height: 300px;"></div>
      </div>

      <div class="chart-card">
        <h3>热门问题TOP10</h3>
        <template v-if="chartLoading">
          <el-skeleton :rows="8" animated />
        </template>
        <el-table v-else :data="store.stats.hotQuestions" style="width: 100%">
          <el-table-column prop="rank" label="排名" width="80" />
          <el-table-column prop="question" label="问题内容" />
          <el-table-column prop="count" label="出现次数" width="120" />
          <el-table-column label="占比" width="180">
            <template #default="{ row }">
              <el-progress :percentage="row.percentage" :show-text="false" />
              {{ row.percentage }}%
            </template>
          </el-table-column>
        </el-table>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted, watch, onBeforeUnmount } from 'vue'
// 通过 API 封装方式按需加载 ECharts
import { createChart, setChartOption, disposeChart } from '@/utils/echarts-api'
import { useConversationStore } from '@/stores/session'

// store & refs
const store = useConversationStore()
const userActivityChart = ref(null)
const timeRange = ref([])
const chartLoading = ref(true)

// 内部变量
let chartInstance = null
let resizeHandler = null

const initChart = async () => {
  if (!userActivityChart.value) return
  chartInstance = await createChart(userActivityChart.value, {
    tooltip: { trigger: 'axis' },
    legend: { data: ['活跃用户数'] },
    xAxis: { type: 'category', data: [] },
    yAxis: { type: 'value' },
    series: [
      {
        name: '活跃用户数',
        type: 'line',
        data: [],
        smooth: true,
        itemStyle: { color: '#409EFF' }
      }
    ]
  })
  resizeHandler = () => chartInstance && chartInstance.resize()
  window.addEventListener('resize', resizeHandler)
}

const updateChart = () => {
  if (!chartInstance || !store.stats.userActivity.length) return
  const xData = store.stats.userActivity.map(item => item.date)
  const yData = store.stats.userActivity.map(item => item.count)
  setChartOption(chartInstance, { xAxis: { data: xData }, series: [{ data: yData }] })
}

const fetchData = () => {
  const params = {
    startTime: timeRange.value?.[0] || '',
    endTime: timeRange.value?.[1] || ''
  }
  store.fetchStats(params)
}

onMounted(async () => {
  try { await initChart(); fetchData() } finally { chartLoading.value = false }
})

watch(() => store.stats.userActivity, updateChart)

onBeforeUnmount(() => {
  if (resizeHandler) window.removeEventListener('resize', resizeHandler)
  if (chartInstance) disposeChart(chartInstance)
})
</script>

<style scoped>
.stats-view {
  padding: 24px;
  background: linear-gradient(135deg, rgba(0, 20, 40, 0.9) 0%, rgba(0, 40, 80, 0.9) 100%);
  min-height: calc(100vh - 48px);
}

.filter-container {
  margin-bottom: 24px;
  padding: 20px;
  background: linear-gradient(135deg, rgba(0, 20, 40, 0.9) 0%, rgba(0, 40, 80, 0.9) 100%);
  border-radius: 12px;
  box-shadow: 0 4px 20px rgba(0, 212, 255, 0.2);
  border: 1px solid rgba(0, 212, 255, 0.3);
  transition: all 0.3s ease;
  backdrop-filter: blur(20px);
}

.filter-container:hover {
  box-shadow: 0 6px 24px rgba(0, 212, 255, 0.3);
  border-color: rgba(0, 212, 255, 0.5);
}

.charts-container {
  display: grid;
  grid-template-columns: 1fr;
  gap: 24px;
}

.chart-card {
  padding: 20px;
  background: linear-gradient(135deg, rgba(0, 20, 40, 0.9) 0%, rgba(0, 40, 80, 0.9) 100%);
  border-radius: 12px;
  box-shadow: 0 4px 20px rgba(0, 212, 255, 0.2);
  border: 1px solid rgba(0, 212, 255, 0.3);
  transition: all 0.3s ease;
  animation: fadeIn 0.5s ease;
  backdrop-filter: blur(20px);
}

.chart-card:hover {
  box-shadow: 0 6px 24px rgba(0, 212, 255, 0.3);
  border-color: rgba(0, 212, 255, 0.5);
}

.chart-card h3 {
  margin-top: 0;
  margin-bottom: 20px;
  font-size: 16px;
  font-weight: 600;
  color: #ffffff;
  display: flex;
  align-items: center;
  text-shadow: 0 1px 2px rgba(0, 0, 0, 0.3);
}

.chart-card h3::before {
  content: "";
  display: inline-block;
  width: 4px;
  height: 16px;
  background: linear-gradient(180deg, #00d4ff 0%, #0099cc 100%);
  border-radius: 2px;
  margin-right: 10px;
}

:deep(.el-table) {
  border-radius: 8px;
  overflow: hidden;
  background: linear-gradient(135deg, rgba(0, 20, 40, 0.9) 0%, rgba(0, 40, 80, 0.9) 100%) !important;
  color: #ffffff !important;
  border: 1px solid rgba(0, 212, 255, 0.3) !important;
}

:deep(.el-table .el-table__cell) {
  padding: 12px 0;
  background: linear-gradient(135deg, rgba(0, 20, 40, 0.9) 0%, rgba(0, 40, 80, 0.9) 100%) !important;
  color: #ffffff !important;
  border-bottom: 1px solid rgba(0, 212, 255, 0.2) !important;
  text-shadow: 0 1px 2px rgba(0, 0, 0, 0.3) !important;
}

:deep(.el-table th) {
  background: rgba(0, 212, 255, 0.25) !important;
  color: #ffffff !important;
  border-bottom: 1px solid rgba(0, 212, 255, 0.4) !important;
  text-shadow: 0 1px 2px rgba(0, 0, 0, 0.5) !important;
}

:deep(.el-table tr:hover > td) {
  background: linear-gradient(135deg, rgba(0, 20, 40, 0.9) 0%, rgba(0, 40, 80, 0.9) 100%) !important;
  border-color: rgba(0, 212, 255, 0.4) !important;
  box-shadow: 0 2px 8px rgba(0, 212, 255, 0.2) !important;
  color: #ffffff !important;
}

:deep(.el-progress-bar) {
  margin-right: 10px;
  width: 100px;
}

/* 深色主题表单样式 */
:deep(.el-form) {
  display: flex;
  align-items: center;
  gap: 16px;
  flex-wrap: wrap;
}

:deep(.el-form-item__label) {
  color: #ffffff !important;
  font-weight: 500 !important;
  text-shadow: 0 1px 2px rgba(0, 0, 0, 0.3) !important;
}

:deep(.el-input__wrapper) {
  background: linear-gradient(135deg, rgba(0, 20, 40, 0.8) 0%, rgba(0, 40, 80, 0.8) 100%) !important;
  border: 1px solid rgba(0, 212, 255, 0.3) !important;
  border-radius: 8px !important;
  backdrop-filter: blur(10px);
}

:deep(.el-input__wrapper:hover) {
  border-color: rgba(0, 212, 255, 0.5) !important;
}

:deep(.el-input__wrapper.is-focus) {
  border-color: rgba(0, 212, 255, 0.6) !important;
  box-shadow: 0 0 0 2px rgba(0, 212, 255, 0.2) !important;
}

:deep(.el-input__inner) {
  color: #ffffff !important;
  background: transparent !important;
  font-weight: 500 !important;
  text-shadow: 0 1px 2px rgba(0, 0, 0, 0.3) !important;
}

:deep(.el-input__inner::placeholder) {
  color: rgba(255, 255, 255, 0.6) !important;
}

:deep(.el-button) {
  background: rgba(0, 212, 255, 0.25) !important;
  border: 1px solid rgba(0, 212, 255, 0.4) !important;
  color: #ffffff !important;
  border-radius: 8px !important;
  backdrop-filter: blur(10px);
  font-weight: 500 !important;
  text-shadow: 0 1px 2px rgba(0, 0, 0, 0.3) !important;
}

:deep(.el-button:hover) {
  background: linear-gradient(135deg, rgba(0, 20, 40, 0.9) 0%, rgba(0, 40, 80, 0.9) 100%) !important;
  border-color: rgba(0, 212, 255, 0.5) !important;
  transform: translateY(-1px);
  box-shadow: 0 4px 12px rgba(0, 212, 255, 0.2) !important;
}

:deep(.el-button--primary) {
  background: linear-gradient(135deg, #00d4ff 0%, #0099cc 100%) !important;
  border: none !important;
  box-shadow: 0 4px 12px rgba(0, 212, 255, 0.3) !important;
}

:deep(.el-button--primary:hover) {
  background: linear-gradient(135deg, #0099cc 0%, #0077a3 100%) !important;
  box-shadow: 0 6px 16px rgba(0, 212, 255, 0.4) !important;
}

/* 自定义No Data状态样式 */
:deep(.el-table__empty-block) {
  background: linear-gradient(135deg, rgba(0, 212, 255, 0.2) 0%, rgba(0, 153, 204, 0.2) 100%) !important;
  border: 1px solid rgba(0, 212, 255, 0.4) !important;
}

:deep(.el-table__empty-text) {
  color: #ffffff !important;
  font-weight: 500 !important;
  text-shadow: 0 1px 2px rgba(0, 0, 0, 0.3) !important;
  font-size: 14px !important;
}

@keyframes fadeIn {
  from {
    opacity: 0;
    transform: translateY(10px);
  }

  to {
    opacity: 1;
    transform: translateY(0);
  }
}

@media (max-width: 768px) {
  .charts-container {
    grid-template-columns: 1fr;
  }

  .chart-card {
    padding: 16px;
  }
}
</style>